package com.xiong.xtools.slip;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.xiong.xtools.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
/**
 * Author : 熊文强
 * Time : 2017/9/29
 * Content : 仿QQ左滑删除，置顶
 */
public class SlipActivity extends AppCompatActivity {
	@BindView(R.id.myListView)
	ListView myListView;

	List<String> data = new ArrayList<>();
	private MyAdapter adapter;
	private Set<SwipeListLayout> sets = new HashSet();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_slip);
		ButterKnife.bind(this);

		initData();
		setAdapter();
		setListener();
	}

	private void setListener() {
		myListView.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				switch (scrollState){
					case SCROLL_STATE_TOUCH_SCROLL:
						if (sets.size() > 0) {
							for (SwipeListLayout s : sets) {
								s.setStatus(SwipeListLayout.Status.Close, true);
								sets.remove(s);
							}
						}
						break;
				}
			}

			@Override
			public void onScroll(AbsListView absListView, int i, int i1, int i2) {

			}
		});
	}

	class MyOnSlipStatusListener implements SwipeListLayout.OnSwipeStatusListener{
		private SwipeListLayout slipListLayout;

		public MyOnSlipStatusListener(SwipeListLayout slipListLayout) {
			this.slipListLayout = slipListLayout;
		}
		@Override
		public void onStatusChange(SwipeListLayout.Status status) {
			if (status == SwipeListLayout.Status.Open) {
				//若有其他的item的状态为Open，则Close，然后移除
				if (sets.size() > 0) {
					for (SwipeListLayout s : sets) {
						s.setStatus(SwipeListLayout.Status.Close, true);
						sets.remove(s);
					}
				}
				sets.add(slipListLayout);
			} else {
				if (sets.contains(slipListLayout))
					sets.remove(slipListLayout);
			}
		}

		@Override
		public void onStartCloseAnimation() {

		}

		@Override
		public void onStartOpenAnimation() {

		}
	}

	private void setAdapter() {
		adapter = new MyAdapter(SlipActivity.this);
		myListView.setAdapter(adapter);
	}

	private void initData() {
		for (int i = 0; i < 15; i++) {
			data.add(i + "");
		}
	}

	class MyAdapter extends BaseAdapter {
		private Context context;
		private LayoutInflater inflater;

		public MyAdapter(Context context) {
			this.context = context;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int i) {
			return data.get(i);
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup viewGroup) {
			final ViewHolder holder;
			if (convertView == null){
				convertView = inflater.inflate(R.layout.silp_item_layout,null);
				holder = new ViewHolder(convertView);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_name.setText(data.get(position));
			holder.sll_main.setOnSwipeStatusListener(new MyOnSlipStatusListener(holder.sll_main));

			holder.tv_top.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					holder.sll_main.setStatus(SwipeListLayout.Status.Close,true);
					String str = data.get(position);
					data.remove(position);
					data.add(0,str);
					notifyDataSetChanged();
				}
			});

			holder.tv_delete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					holder.sll_main.setStatus(SwipeListLayout.Status.Close,true);
					data.remove(position);
					notifyDataSetChanged();
				}
			});

			return convertView;
		}

	}
	static class ViewHolder{
		@BindView(R.id.sll_main)
		SwipeListLayout sll_main;
		@BindView(R.id.tv_top)
		TextView tv_top;
		@BindView(R.id.tv_delete)
		TextView tv_delete;
		@BindView(R.id.tv_name)
		TextView tv_name;

		public ViewHolder(View view){
			ButterKnife.bind(this,view);
		}
	}
}
